package routes

import (
	"github.com/gin-gonic/gin"
	"github.com/zmqge/vireo-gin-admin/pkg/middleware"
	"gorm.io/gorm"
	"github.com/zmqge/vireo-gin-admin/app/admin/controllers"
	"github.com/zmqge/vireo-gin-admin/app/admin/services"
	"github.com/zmqge/vireo-gin-admin/app/admin/repositories"
)

func RegisterAdminRoutes(engine *gin.Engine, db *gorm.DB) {
	userService := services.NewUserService(db)
	tokenService := services.NewTokenService()
	authController := controllers.NewAuthController(userService, tokenService)
	deptController := controllers.NewDeptController(db)
	dictRepository := repositories.NewDictRepository(db)
	dictService := services.NewDictService(dictRepository)
	dictController := controllers.NewDictController(dictService)
	menuController := controllers.NewMenuController(db)
	roleController := controllers.NewRoleController(db)
	userController := controllers.NewUserController(userService)
	configRepository := repositories.NewConfigRepository(db)
	configService := services.NewConfigService(configRepository)
	configController := controllers.NewConfigController(configService)
	fileRepository := repositories.NewFileRepository(db)
	fileService := services.NewFileService(fileRepository)
	fileController := controllers.NewFileController(fileService)
	noticesRepository := repositories.NewNoticesRepository(db)
	userRepository := repositories.NewUserRepository(db)
	noticeReceiverRepository := repositories.NewNoticeReceiverRepository(db)
	noticesService := services.NewNoticesService(noticesRepository, userRepository, noticeReceiverRepository)
	noticesController := controllers.NewNoticesController(noticesService)
	permissionController := controllers.NewPermissionController()
	groupapi_v1_auth := engine.Group("/api/v1/auth")
	{
	groupapi_v1_auth.POST("/login", authController.Login)
	groupapi_v1_auth.GET("/captcha", authController.GetCaptcha)
	groupapi_v1_auth.DELETE("/logout", middleware.JWT(), authController.Logout)
	groupapi_v1_auth.POST("/refresh-token", authController.RefreshToken)
	}
	groupapi_v1 := engine.Group("/api/v1")
	{
	groupapi_v1.GET("/config/:id", middleware.JWT(), middleware.RBAC("sys:config:view"), middleware.DATAPERM(), configController.GetConfigDetails)
	groupapi_v1.GET("/config/page", middleware.JWT(), middleware.RBAC("sys:config:query"), middleware.DATAPERM(), configController.ListConfigs)
	groupapi_v1.POST("/config", middleware.JWT(), middleware.RBAC("sys:config:add"), configController.CreateConfig)
	groupapi_v1.PUT("/config/:id", middleware.JWT(), middleware.RBAC("sys:config:update"), middleware.DATAPERM(), configController.UpdateConfig)
	groupapi_v1.DELETE("/config/:id", middleware.JWT(), middleware.RBAC("sys:config:delete"), configController.DeleteConfig)
	groupapi_v1.GET("/config/:id/form", middleware.JWT(), middleware.RBAC("sys:config:details"), middleware.DATAPERM(), configController.GetConfigForm)
	groupapi_v1.POST("/dept", middleware.JWT(), middleware.RBAC("sys:dept:add"), deptController.CreateDept)
	groupapi_v1.PUT("/dept/:id", middleware.JWT(), middleware.RBAC("sys:dept:edit"), deptController.UpdateDept)
	groupapi_v1.GET("/dept/:id/form", middleware.JWT(), middleware.RBAC("sys:dept:view"), deptController.GetDept)
	groupapi_v1.GET("/dept/options", middleware.JWT(), middleware.RBAC("sys:dept:options"), middleware.DATAPERM(), deptController.GetDeptOptions)
	groupapi_v1.GET("/dept", middleware.JWT(), middleware.RBAC("sys:dept:query"), deptController.GetDepts)
	groupapi_v1.DELETE("/dept/:id", middleware.JWT(), middleware.RBAC("sys:dept:delete"), deptController.DeleteDept)
	groupapi_v1.GET("/dicts/:id/form", middleware.JWT(), middleware.RBAC("sys:dict:details"), dictController.GetDict)
	groupapi_v1.GET("/dicts/page", middleware.JWT(), middleware.RBAC("sys:dict:query"), dictController.ListDicts)
	groupapi_v1.POST("/dicts", middleware.JWT(), middleware.RBAC("sys:dict:add"), dictController.CreateDict)
	groupapi_v1.PUT("/dicts/:id", middleware.JWT(), middleware.RBAC("sys:dict:edit"), dictController.UpdateDict)
	groupapi_v1.DELETE("/dicts/:id", middleware.JWT(), middleware.RBAC("sys:dict-item:delete"), dictController.DeleteDict)
	groupapi_v1.GET("/dicts-items/:dictCode/items", middleware.JWT(), middleware.RBAC("sys:dict-item:details"), dictController.GetDictItem)
	groupapi_v1.GET("/dicts-items/:dictCode/items/page", middleware.JWT(), middleware.RBAC("sys:dict-item:query"), dictController.GetDictItemPage)
	groupapi_v1.POST("/dicts-items/:dictCode/items", middleware.JWT(), middleware.RBAC("sys:dict-item:add"), dictController.CreateDictItem)
	groupapi_v1.PUT("/dicts-items/:dictCode/items/:id", middleware.JWT(), middleware.RBAC("sys:dict-item:edit"), dictController.UpdateDictItem)
	groupapi_v1.DELETE("/dicts-items/:dictCode/items/:id", middleware.JWT(), middleware.RBAC("sys:dict:delete"), dictController.DeleteDictItem)
	groupapi_v1.GET("/dicts-items/:dictCode/items/:itemId/form", middleware.JWT(), middleware.RBAC("sys:dict-item:form"), dictController.GetDictItemForm)
	groupapi_v1.GET("/file/:id", middleware.JWT(), middleware.RBAC("sys:file:view"), middleware.DATAPERM(), fileController.GetFileDetails)
	groupapi_v1.GET("/file/page", middleware.JWT(), middleware.RBAC("sys:file:query"), middleware.DATAPERM(), fileController.ListFiles)
	groupapi_v1.POST("/files", middleware.JWT(), middleware.RBAC("sys:file:add"), fileController.UploadFile)
	groupapi_v1.DELETE("/file/:id", middleware.JWT(), middleware.RBAC("sys:file:delete"), fileController.DeleteFile)
	groupapi_v1.GET("/file/:id/form", middleware.JWT(), middleware.RBAC("sys:file:details"), middleware.DATAPERM(), fileController.GetFileForm)
	groupapi_v1.GET("/menus/routes", middleware.JWT(), menuController.GetCurrentUserRoutes)
	groupapi_v1.GET("/menus", middleware.JWT(), middleware.RBAC("sys:menu:query"), menuController.ListMenus)
	groupapi_v1.GET("/menus/options", middleware.JWT(), middleware.RBAC("sys:menu:options"), menuController.ListMenuOptions)
	groupapi_v1.GET("/menus/:id/form", middleware.JWT(), middleware.RBAC("sys:menu:details"), menuController.GetMenuDetail)
	groupapi_v1.POST("/menus", middleware.JWT(), middleware.RBAC("sys:menu:add"), menuController.AddMenu)
	groupapi_v1.PUT("/menus/:id", middleware.JWT(), middleware.RBAC("sys:menu:edit"), menuController.UpdateMenu)
	groupapi_v1.DELETE("/menus/:id", middleware.JWT(), middleware.RBAC("sys:menu:delete"), menuController.DeleteMenu)
	groupapi_v1.GET("/notices/:id/detail", middleware.JWT(), middleware.RBAC("sys:notice:detail"), middleware.DATAPERM(), noticesController.GetNoticesDetails)
	groupapi_v1.GET("/notices/:id/my-detail", middleware.JWT(), middleware.RBAC("sys:notice:my-detail"), noticesController.GetMyNoticesDetails)
	groupapi_v1.GET("/notices/page", middleware.JWT(), middleware.RBAC("sys:notice:query"), middleware.DATAPERM(), noticesController.ListNoticess)
	groupapi_v1.POST("/notices", middleware.JWT(), middleware.RBAC("sys:notice:add"), noticesController.CreateNotices)
	groupapi_v1.PUT("/notices/:id", middleware.JWT(), middleware.RBAC("sys:notice:update"), middleware.DATAPERM(), noticesController.UpdateNotices)
	groupapi_v1.DELETE("/notices/:id", middleware.JWT(), middleware.RBAC("sys:notice:delete"), noticesController.DeleteNotices)
	groupapi_v1.GET("/notices/:id/form", middleware.JWT(), middleware.RBAC("sys:notice:form"), middleware.DATAPERM(), noticesController.GetNoticesForm)
	groupapi_v1.PUT("/notices/:id/revoke", middleware.JWT(), middleware.RBAC("sys:notice:revoke"), noticesController.RevokeNotice)
	groupapi_v1.PUT("/notices/:id/publish", middleware.JWT(), middleware.RBAC("sys:notice:publish"), noticesController.PublishNotice)
	groupapi_v1.GET("/notices/my-page", middleware.JWT(), middleware.DATAPERM(), noticesController.GetMyNoticess)
	groupapi_v1.PUT("/notices/my-page/read-all", middleware.JWT(), middleware.RBAC("sys:notice:read-all"), noticesController.MarkAllAsRead)
	groupapi_v1.GET("/perms/options", middleware.JWT(), middleware.RBAC("sys:perm:options"), permissionController.ListPermOptions)
	groupapi_v1.POST("/roles", middleware.JWT(), middleware.RBAC("sys:role:add"), roleController.Create)
	groupapi_v1.PUT("/roles/:id", middleware.JWT(), middleware.RBAC("sys:role:edit"), roleController.UpdateRole)
	groupapi_v1.DELETE("/roles/:id", middleware.JWT(), middleware.RBAC("sys:role:delete"), roleController.DeleteRole)
	groupapi_v1.GET("/roles/:id/form", middleware.JWT(), middleware.RBAC("sys:role:detail"), roleController.GetRoleDetail)
	groupapi_v1.GET("/roles/page", middleware.JWT(), middleware.RBAC("sys:role:query"), roleController.List)
	groupapi_v1.GET("/roles/:id/menuIds", middleware.JWT(), middleware.RBAC("sys:role:menu"), roleController.GetRoleMenus)
	groupapi_v1.GET("/roles/:id/permCodes", middleware.JWT(), middleware.RBAC("sys:role:perm"), roleController.GetRolePerms)
	groupapi_v1.PUT("/roles/:id/menus", middleware.JWT(), middleware.RBAC("sys:role:menu:update"), roleController.UpdateRoleMenus)
	groupapi_v1.PUT("/roles/:id/perms", middleware.JWT(), middleware.RBAC("sys:role:perm:update"), roleController.UpdateRolePerms)
	groupapi_v1.GET("/roles/options", middleware.JWT(), middleware.RBAC("sys:role:options"), roleController.ListRoleOptions)
	groupapi_v1.GET("users/me", middleware.JWT(), userController.Me)
	groupapi_v1.DELETE("/users/:id", middleware.JWT(), middleware.RBAC("sys:user:delete"), userController.Delete)
	groupapi_v1.GET("users/page", middleware.JWT(), middleware.RBAC("sys:user:page"), middleware.DATAPERM(), userController.GetUserPage)
	groupapi_v1.GET("/users/:id/form", middleware.JWT(), middleware.RBAC("sys:user:info"), userController.GetUser)
	groupapi_v1.PUT("/users/:id", middleware.JWT(), middleware.RBAC("sys:user:edit"), userController.UpdateUser)
	groupapi_v1.POST("/users", middleware.JWT(), middleware.RBAC("sys:user:add"), userController.CreateUser)
	groupapi_v1.PUT("/users/:id/password/reset", middleware.JWT(), middleware.RBAC("sys:user:reset-password"), userController.ResetPassword)
	groupapi_v1.GET("/users/profile", middleware.JWT(), middleware.RBAC("sys:user:profile"), userController.GetUserProfile)
	groupapi_v1.PUT("/users/password", middleware.JWT(), middleware.RBAC("sys:user:change-password"), userController.ChangePassword)
	groupapi_v1.PUT("/users/profile", middleware.JWT(), middleware.RBAC("sys:user:update-profile"), userController.UpdateMyProfile)
	groupapi_v1.GET("/users/options", middleware.JWT(), middleware.RBAC("sys:user:options"), middleware.DATAPERM(), userController.ListUserOptions)
	}
}
